<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
class CustomerController
{

     /*
      * 客户名称查找
      * customer_name
      * */
     public function customer_list(){
            $customer = DB::table('customer')
                ->select('id','customer_name','telephone','address','balance')
                ->where('servicer_id',$_SESSION['servicer_id'])
                ->where('status',0)
                ->orderBy('id','desc')
                ->get();
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = $customer;
           return json_encode($res,1);
     }

     /*客户下单
      * */
     public function customer_order_add(Request $request){
             $list = $request->product;
             $list = json_decode($list,1);
             $totol = 0;
             $product_num = 0;
             $product_id = '';
             $order_product_id = '';
             $product = [];
             $company_id = 0;
             foreach ($list as $key=>$value){
                    $product[$key]['category'] = $value['name'];
                    $product[$key]['brand'] = $value['brand'];
                    $product[$key]['standards'] = $value['norms'];
                    $product[$key]['pack_method'] = $value['package'];
                    $product[$key]['supplyer_company'] = $request->supplyer_company;
                    $product[$key]['pay_address'] = $request->pay_address;
                    $product[$key]['pay_name'] = $request->pay_name;
                    //下面这些查找去掉
                    $product[$key]['product_num'] = $value['count'];
                    $product[$key]['price'] = $value['money'];
             }

             foreach ($product as $key=>$value){
                 $data = DB::table('product')
                     ->where('category',$value['category'])
                     ->where('brand',$value['brand'])
                     ->where('standards',$value['standards'])
                     ->where('pack_method',$value['pack_method'])
                     ->where('supplyer_company',$value['supplyer_company'])
                     ->where('pay_address',$value['pay_address'])
                     ->where('pay_name',$value['pay_name'])
                     ->first();
                  if(empty($data)){
                       $res['status'] = false;
                       $res['code'] = 400;
                       $res['msg'] = '';
                       $res['data'] = [];
                       return response()->json($res);
                  }
                  $data = (array)$data;
                  $company_id =  $data['company_id'];
                  $data['product_id'] = $data['id'];
                  $data['price'] = $value['price'];
                  $data['product_num'] = $value['product_num'];
                  $data['total_sell'] = $value['price']*$value['product_num'];
                  $data['customer_id'] = $request->customer_id;
                  $data['servicer_id'] = $_SESSION['servicer_id'];
                  $data['mark']  = $request->mark;
                  $data['site_address'] = $request->site_address;
                  $data['get_address'] = $request->get_address;
                  $data['get_position'] = $request->get_position;

                  $data['create_time'] = time();
                  $data['create_date'] = date('Y-m-d H:i:s',time());
                  $totol += $data['total_sell'];
                  $product_num = $product_num + $value['product_num'];
                  $product_id .= $data['id'].',';
                  unset($data['id']);
                  $id = DB::table('order_product')->insertGetId($data);
                  $order_product_id .= $id.',';

             }

             $customer = DB::table('customer')->select('id','customer_name','telephone','balance')->where('id',$request->customer_id)->first();


             $order['customer_id'] = $request->customer_id;
             $order['end_time'] = $request->end_time.' 00:00:00';
             $order['servicer_mark'] = $request->mark;
             $order['product_id'] = $product_id;
             $order['total_sell'] = $totol;
             $order['pay_address'] = $request->pay_address;
             $order['company_id'] = $company_id;
             $order['company_name'] = $request->pay_name;
             $order['get_address'] = $request->get_address;
             $order['get_position'] = $request->get_position;
             $order['customer_name'] = $customer->customer_name;
             $order['customer_phone'] = $customer->telephone;
             $order['take_name'] = $request->take_name;
             $order['take_phone'] = $request->take_phone;
             //这里录入一个产品
             $order['standards'] = $product[0]['standards'];
             $order['product_num'] = $product_num;
             $order['pack_method'] = $product[0]['pack_method'];
             $order['category'] = $product[0]['category'];
             $order['brand'] = $product[0]['brand'];
             $order['product_name'] = $product[0]['brand'].$product[0]['category'];
             $order['unit_price'] = $product[0]['price'];

             $order['supplyer_company'] = $request->supplyer_company;

             $position = json_decode($request->get_position);
             //\Log::debug($position);
             $order['position'] = $position[1];
             $order['order_product_id'] = $order_product_id;
             $order['order_code'] = date('YmdHis',time()).rand(1000000,99999999);
             $order['servicer_id'] = $_SESSION['servicer_id'];
             $order['servicer_name'] = $_SESSION['servicer_name'];
             $order['servicer_phone'] = $_SESSION['servicer_phone'];
             $order['make_date'] = date('Y-m-d H:i:s',time());
             $order['create_time'] = time();

             if($request->car_info==null) {
                 $order['traffic_method'] = 0;
                 $order['get_method'] = '商家送货';
             }else{
                 $order['get_method'] = '客户自提';
                 $order['traffic_method'] = 1;
                 $order['car_info'] = $request->car_info;
             }
             $order_id = DB::table('order')->insertGetId($order);

              //准备开始预付款
             if($customer->balance > 0){
              $this->re_pay($customer,$totol,$request->pay_name,$order_id);
             }

             $agreement['order_id'] = $order_id;
             $agreement['customer_id'] = $request->customer_id;
             $agreement['servicer_id'] = $_SESSION['servicer_id'];
             $agreement['product_id'] = $product_id;
             $agreement['total_sell'] = $totol;

             $agreement['site_address'] = $request->site_address;
             $agreement['get_address'] = $request->get_address;
             $agreement['get_position'] = $request->get_position;

             $agreement['take_name'] = $request->take_name;
             $agreement['take_phone'] = $request->take_phone;

             $agreement['customer_id'] = $request->customer_id;
             $agreement['customer_name'] = $customer->customer_name;
             $agreement['take_phone'] = $request->take_phone;

             $agreement['order_product_id'] = $order_product_id;
             $agreement['agreement_order'] = $order['order_code'];
             $agreement['agreement_date'] = date('Y-m-d',time());
             $agreement['create_date'] = date('Y-m-d H:i:s',time());
             $agreement['create_time'] = time();
             $agreement['status'] = 0;

             //\Log::debug($agreement);

             $agreement_id = DB::table('agreement')->insertGetId($agreement);

             DB::table('order')->where('id',$order_id)
                 ->update(array('agreement_id'=>$agreement_id));

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = [];
           return json_encode($res,1);

     }

     /*预付款
      * */
     public function re_pay($customer,$totol,$get_company,$order_id){
         $order = DB::table('order')
                    ->select('id','total_sell','already_pay','pay_id','pay_status')
                    ->where('id',$order_id)->first();
         if($order->pay_status == 0) {
             if($customer->balance > $totol) {
                 $balance = $customer->balance - $order->total_sell + $order->already_pay;
                 DB::table('customer')->where('id', $customer->id)->update(array('balance' => $balance));

                 $pay['customer_id'] = $customer->id;
                 $pay['customer_name'] = $customer->customer_name;
                 $pay['balance'] = $balance;
                 $pay['re_balance'] = $customer->balance;
                 $pay['proof_pic'] = 'repay.jpg';
                 $pay['pay_money'] = $totol;
                 $pay['date'] = date('Y-m-d', time());
                 $pay['pay_method'] = '预付款';
                 $pay['get_company'] = $get_company;
                 $pay['order_id'] = $order_id;
                 $pay['create_time'] = time();
                 $pay['create_date'] = date('Y-m-d H:i:s', time());
                 $pay_id = DB::table('pay')->insertGetId($pay);

                 $order_data['pay_id'] = $pay_id.',';
                 $order_data['already_pay'] =$totol;
                 $order_data['pay_status'] =1;
                DB::table('order')->where('id',$order_id)->update($order_data);
             }else{
                 $balance = 0;
                 DB::table('customer')->where('id', $customer->id)->update(array('balance' => $balance));
                 $pay['customer_id'] = $customer->id;
                 $pay['customer_name'] = $customer->customer_name;
                 $pay['balance'] = 0;
                 $pay['re_balance'] = $customer->balance;
                 $pay['proof_pic'] = 'repay.jpg';
                 $pay['pay_money'] = $totol;
                 $pay['date'] = date('Y-m-d', time());
                 $pay['pay_method'] = '预付款';
                 $pay['get_company'] = $get_company;
                 $pay['order_id'] = $order_id;
                 $pay['create_time'] = time();
                 $pay['create_date'] = date('Y-m-d H:i:s', time());
                 $pay_id = DB::table('pay')->insertGetId($pay);
                 $order_data['pay_id'] = $pay_id.',';
                 $order_data['already_pay'] = $customer->balance;
                DB::table('order')->where('id',$order_id)->update($order_data);
             }
         }

     }


     /*
      * 客户下单
      * */

     public function customer_order_msg(){
           $customer = DB::table('customer')
              ->select('id','customer_name','telephone','address')
              ->where('status',0)
              ->where('servicer_id',$_SESSION['servicer_id'])->get();
           $customer = json_decode($customer,1);
           $product = DB::table('product')
               ->select('id','category','brand','standards','pack_method','supplyer_company','pay_address','pay_name')
               ->get();
           //$product = json_decode($product,1);
           $pay_name['name'] = [];
           foreach ($product as $key=>$value){
                   if(!in_array($value->pay_name,$pay_name['name'])) {
                    $pay_name['name'][] = $value->pay_name;
                    }
                   $pay_name[$value->pay_name]['supplyer_company'] = [];
                   $product_list[$value->supplyer_company]['category'] = [];
                   $product_list[$value->supplyer_company][$value->category]['brand'] = [];
                   $product_list[$value->supplyer_company][$value->category]['standards'] = [];
                   $product_list[$value->supplyer_company][$value->category]['pack_method'] = [];
                   $product_list[$value->supplyer_company][$value->category]['supplyer_company'] = [];
                   $product_list[$value->supplyer_company][$value->category]['pay_address'] = [];
                   $product_list[$value->supplyer_company][$value->category]['pay_name'] = [];

           }
           foreach ($product as $key=>$value){

                if(!in_array($value->supplyer_company,$pay_name[$value->pay_name]['supplyer_company'])) {
                    $pay_name[$value->pay_name]['supplyer_company'][] = $value->supplyer_company;
                    $pay_name[$value->pay_name][$value->supplyer_company][] = $value->pay_address;
                }
                if(!in_array($value->category,$product_list[$value->supplyer_company]['category'])) {
                    $product_list[$value->supplyer_company]['category'][] = $value->category;
                }
                if(!in_array($value->brand,$product_list[$value->supplyer_company][$value->category]['brand'])){
                    $product_list[$value->supplyer_company][$value->category]['brand'][] = $value->brand;
                }
                if(!in_array($value->standards,$product_list[$value->supplyer_company][$value->category]['standards'])){
                    $product_list[$value->supplyer_company][$value->category]['standards'][] = $value->standards;
                }
                if(!in_array($value->pack_method,$product_list[$value->supplyer_company][$value->category]['pack_method'])){
                    $product_list[$value->supplyer_company][$value->category]['pack_method'][] = $value->pack_method;
                }

           }
         $res['status'] = true;
         $res['code'] = 200;
         $res['msg'] = '';
         $res['data']['customer'] =$customer;
         $res['data']['pay_name'] =$pay_name;
         $res['data']['product'] = $product_list;
         return json_encode($res,1);
     }


     /*
      * agreement_id
      * */
     public function agreement(Request $request){

         if(!isset($request->agreement_id)){
             $agreement = DB::table('agreement')
                 ->select('id','order_product_id')
                 ->where('status',0)
                 ->where('servicer_id',$_SESSION['servicer_id'])
                 ->first();

              $order_product_id = explode(',', $agreement->order_product_id);
              if(!empty($order_product_id)) {
                  $order_product = DB::table('order_product')
                      ->select('id', 'brand', 'standards', 'product_address', 'price', 'product_num', 'total_sell', 'mark')
                      ->where('servicer_id', $_SESSION['servicer_id'])
                      ->whereIn('id', $order_product_id)->get();

                  $res['status'] = true;
                  $res['code'] = 200;
                  $res['msg'] = '';
                  $res['data']['agreement_id'] = $agreement->id;
                  $res['data']['product'] = $order_product;
                  return json_encode($res, 1);
              }else{
                  $res['status'] = false;
                  $res['code'] = 400;
                  $res['msg'] = '';
                  $res['data'] = [];
              }

         }else{
             $agreement = DB::table('agreement')
                    ->select('id','order_product_id')
                    ->where('id',$request->agreement_id)->first();
             $order_product_id = explode(',', $agreement->order_product_id);
             $order_product = DB::table('order_product')
                    ->select('id','brand','standards','product_address','price','product_num','total_sell','mark')
                    ->where('servicer_id',$_SESSION['servicer_id'])
                    ->whereIn('id',$order_product_id)->get();
             $res['status'] = true;
             $res['code'] = 200;
             $res['msg'] = '';
             $res['data']['agreement_id'] = $agreement->id;
             $res['data']['product'] = $order_product;
             return json_encode($res,1);
         }
     }

   /*
    *添加hetong
    *agreement_id
    * */
    public function agreement_add(Request $request)
    {
        $list = $request->all();
        unset($list['token']);
        unset($list['data']);
        foreach ($list as $key=>$value){
            if(empty($value)){
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '你输入有空，请重新填写';
                $res['data']  = [];
                return response()->json($res);
            }
        }
        $sign_agreement_id = DB::table('sign_agreement')->insertGetId($list);
        DB::table('agreement')
            ->where('id',$request->agreement_id)
            ->update(array(
                'agreement_method'=>$request->agreement_method,
                'agreement_code'=>$request->agreement_code,
                'agreement_position'=>$request->agreement_position,
                'supplyer_company'=>$request->supplyer_company,
                'get_company'=>$request->get_company,
                'sign_agreement_id'=>$sign_agreement_id,
                'status'=>1
            ));

           DB::table('order')->where('agreement_id',$request->agreement_id)->update(array(
               'agreement_status'=>1,
               'agreement_code'=>$request->agreement_code
           ));

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = [];
           return json_encode($res,1);
    }

    /*
     *customer_id
     * */

    public function customer_info(Request $request){
           $customer = DB::table('customer')
               ->select('id','customer_name','telephone','address')
               ->where('id',$request->customer_id)->first();


           $agreement = DB::table('agreement')
               ->select('order_id','status')
               ->where('customer_id',$request->customer_id)
               ->where('servicer_id',$_SESSION['servicer_id'])->get();

           $order_id = [];
           foreach ($agreement as $key=>$value){
               $order_id[] = $value->order_id;
           }
           $order = DB::table('order')
               ->select('order_code','make_date','order_product_id','supplyer_company','agreement_status','agreement_id','print_status')
               ->whereIn('id',$order_id)
               ->get();

           $customer_info = [];
           $order = json_decode($order,1);
           foreach ($order as $key=>$value){

               $order_product_id = explode(',',$value['order_product_id']);
               $product = DB::table('order_product')
                    ->select('id','product_name','category','brand','standards','pack_method','product_num')
                    ->whereIn('id',$order_product_id)
                    ->get();

               $customer_info[$key]['make_date'] = $value['make_date'];
               $customer_info[$key]['order_code'] = $value['order_code'];
               $customer_info[$key]['supplyer_company'] = $value['supplyer_company'];
               $customer_info[$key]['agreement_status'] = $value['agreement_status'];
               $customer_info[$key]['print_status'] = $value['print_status'];
               $customer_info[$key]['supplyer_company'] = $value['supplyer_company'];
               $customer_info[$key]['agreement_id'] = $value['agreement_id'];
               $customer_info[$key]['product'] = $product;
           }
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data']['customer'] = $customer;
           $res['data']['customer_info'] = $customer_info;
           return json_encode($res,1);
    }


    /*
     *customer_id
     * */

    public function customer_delete(Request $request){

       $bool = DB::table('customer')
            ->where('id',$request->customer_id)
            ->update(array('status'=>1));

       if($bool){
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = [];
           return json_encode($res,1);
       }else{
           $res['status'] = false;
           $res['code'] = 400;
           $res['msg'] = '';
           $res['data'] = [];
           return json_encode($res,1);
       }

    }





    /*
     *customer_id
     * */

    public function customer_update(Request $request){

           $bool = DB::table('customer')
               ->where('id',$request->customer_id)
               ->update(array('customer_name'=>$request->customer_name,'telephone'=>$request->telephone,'address'=>$request->address));

           if($bool){
               $res['status'] = true;
               $res['code'] = 200;
               $res['msg'] = '';
               $res['data'] = [];
               return json_encode($res,1);
           }else{
               $res['status'] = false;
               $res['code'] = 400;
               $res['msg'] = '';
               $res['data'] = [];
               return json_encode($res,1);
           }
    }





    /*
      * 查看业绩
      *
      * date
      *
      * pack_mathod
      *
      * customer_name
      *
      * */

     public function performan(Request $request){

           $customer = DB::table('customer')
               ->select('id','customer_name','balance')
               ->where('customer_name','like','%'.$request->customer_name.'%')
               ->where('servicer_id',$_SESSION['servicer_id'])
               ->where('status',0)
               ->get();

           $customer_id = [];
           $customer_name = [];
           $customer_balance = [];
           $customer = json_decode($customer,1);
           foreach ($customer as $key=>$value){
                $customer_id[] = $value['id'];
                $customer_name[$value['id']] = $value['customer_name'];
                $customer_balance[$value['id']] = $value['balance'];
           }

           /*
            * 获取一下订单
            * */
           $order = DB::table('order')
                 ->select('id')
                 ->whereIn('customer_id',$customer_id)
                 ->where('code','!=',2)
                 ->get();

           $order_id = [];
           foreach ($order as $key=>$value){
                $order_id[] = $value->id;
           }

           $order_product = DB::table('order_product')
               ->select('product_name','standards','price','category','brand','pack_method','product_num','total_sell','customer_id','create_time')
               ->whereIn('order_id',$order_id)
               ->get();

           $product = [];
           foreach ($order_product as $key=>$value){
               $product[$value->customer_id]['create_time'] = $value->create_time;
               $product[$value->customer_id]['customer_name'] = $customer_name[$value->customer_id];
               $product[$value->customer_id]['owed'] = (int)$customer_balance[$value->customer_id] - (int)$value->total_sell;
               $product[$value->customer_id]['product'][$key] = $value;

           }

           $arr = [];
           foreach ($product as $key=>$value){
            $arr[] = $value;
           }
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data'] = $arr;

                return json_encode($res,1);
     }









}